package me.zhengjie.sqlservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.mapper.WcsLineBodyMapper;
import me.zhengjie.pojo.entity.WcsLineBodyInbound;
import me.zhengjie.sqlservice.WcsLineBodyInboundService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class WcsLineBodyServiceImpl extends ServiceImpl<WcsLineBodyMapper, WcsLineBodyInbound> implements WcsLineBodyInboundService {

    private final WcsLineBodyMapper wcsLineBodyMapper;

    @Override
    public WcsLineBodyInbound selectOne() {
        WcsLineBodyInbound wcsLineBody = wcsLineBodyMapper.selectOne(null);
        return wcsLineBody;
    }

    @Override
    public boolean updateOneWritePlc(String chainMachineStorage, int oneWritePlc) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getOneWritePlc,oneWritePlc)
                .set(WcsLineBodyInbound::getOneTime, LocalDateTime.now())
                .eq(WcsLineBodyInbound::getChainMachineStorage,chainMachineStorage);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateTwoWritePlc(String chainMachineStorage, int twoWritePlc) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getTwoWritePlc,twoWritePlc)
                .set(WcsLineBodyInbound::getTwoTime, LocalDateTime.now())
                .eq(WcsLineBodyInbound::getChainMachineStorage,chainMachineStorage);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateThreeWritePlc(String chainMachineStorage, int threeWritePlc) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getThreeWritePlc,threeWritePlc)
                .set(WcsLineBodyInbound::getThreeTime, LocalDateTime.now())
                .eq(WcsLineBodyInbound::getChainMachineStorage,chainMachineStorage);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public WcsLineBodyInbound selectStorageCode(String storage) {
        LambdaQueryWrapper<WcsLineBodyInbound> wcsLineBodyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineBodyLambdaQueryWrapper.eq(WcsLineBodyInbound::getChainMachineStorage,storage);
        return wcsLineBodyMapper.selectOne(wcsLineBodyLambdaQueryWrapper);
    }

    @Override
    public boolean updateNgWritePlc(String chainMachineStorage, int ngWritePlc) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getNgWritePlc,ngWritePlc)
                .eq(WcsLineBodyInbound::getChainMachineStorage,chainMachineStorage);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public boolean updateHoistRepairPlc(Integer wcsLineBodyId, int status) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getHoistRepairPlc,status)
                .eq(WcsLineBodyInbound::getId,wcsLineBodyId);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public WcsLineBodyInbound selectHoistStorage(String hoistStorage) {
        LambdaQueryWrapper<WcsLineBodyInbound> wcsLineBodyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineBodyLambdaQueryWrapper.eq(WcsLineBodyInbound::getHoistStorage,hoistStorage);
        return wcsLineBodyMapper.selectOne(wcsLineBodyLambdaQueryWrapper);
    }

    @Override
    public boolean updateHoistakePlc(Integer wcsLineBodyId, int status) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyLambdaUpdateWrapper.set(WcsLineBodyInbound::getHoistTakePlc,status)
                .eq(WcsLineBodyInbound::getId,wcsLineBodyId);
        return wcsLineBodyMapper.update(null,wcsLineBodyLambdaUpdateWrapper) >0;
    }

    @Override
    public WcsLineBodyInbound selectChainMachineStorage(String chainMachineStorage) {
        LambdaQueryWrapper<WcsLineBodyInbound> wcsLineBodyInboundLambdaQueryWrapper = new LambdaQueryWrapper<>();
        wcsLineBodyInboundLambdaQueryWrapper.eq(WcsLineBodyInbound::getChainMachineStorage,chainMachineStorage);
        return wcsLineBodyMapper.selectOne(wcsLineBodyInboundLambdaQueryWrapper);
    }

    @Override
    public boolean updateTaskIssueTime(Integer id) {
        LambdaUpdateWrapper<WcsLineBodyInbound> wcsLineBodyInboundLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        wcsLineBodyInboundLambdaUpdateWrapper.set(WcsLineBodyInbound::getTaskIssueTime,LocalDateTime.now())
                .eq(WcsLineBodyInbound::getId,id);
        return wcsLineBodyMapper.update(null,wcsLineBodyInboundLambdaUpdateWrapper) >0;
    }
}
